package com.abvnet.project.water.project.controller;

import com.abvnet.common.utils.file.FileUploadUtils;
import com.abvnet.framework.aspectj.lang.annotation.Log;
import com.abvnet.framework.aspectj.lang.enums.BusinessType;
import com.abvnet.framework.config.AbvnetConfig;
import com.abvnet.framework.web.controller.BaseController;
import com.abvnet.framework.web.domain.AjaxResult;
import com.abvnet.project.water.project.domain.ProjectEqp;
import com.abvnet.project.water.project.service.IProjectEqpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/project/projectEqp")
@Slf4j
public class ProjectEqpController extends BaseController {

    private String prefix = "project/projectInfo";

    @Autowired
    private IProjectEqpService projectEqpService;

    @GetMapping("/edit/{projectId}")
    public String edit(@PathVariable("projectId") Long projectId, ModelMap mmap) {
        mmap.put("projectEqp", projectEqpService.selectProjectEqpByProjectId(projectId));
        return prefix + "/eqp";
    }

    @Log(title = "设备运行情况", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProjectEqp projectEqp, @RequestParam("aerationEqpFile") MultipartFile aerationEqpFile,
                               @RequestParam("diveEqpFile") MultipartFile diveEqpFile,
                               @RequestParam("sewagePumpFile") MultipartFile sewagePumpFile,
                               @RequestParam("sludgePumpFile") MultipartFile sludgePumpFile,
                               @RequestParam("airFloatFile") MultipartFile airFloatFile,
                               @RequestParam("mbrMemFile") MultipartFile mbrMemFile,
                               @RequestParam("otherEqpFile") MultipartFile otherEqpFile) {
        try {
            if (aerationEqpFile != null && !aerationEqpFile.isEmpty()) {
                String aerationUrl = FileUploadUtils.upload(AbvnetConfig.getEqpPath(), aerationEqpFile);
                projectEqp.setAerationEqpUrl(aerationUrl);
            }
            if (diveEqpFile != null && !diveEqpFile.isEmpty()) {
                String diveUrl = FileUploadUtils.upload(AbvnetConfig.getEqpPath(), diveEqpFile);
                projectEqp.setDiveEqpUrl(diveUrl);
            }
            if (sewagePumpFile != null && !sewagePumpFile.isEmpty()) {
                String sewagePumpUrl = FileUploadUtils.upload(AbvnetConfig.getEqpPath(), sewagePumpFile);
                projectEqp.setSewagePumpUrl(sewagePumpUrl);
            }
            if (sludgePumpFile != null && !sludgePumpFile.isEmpty()) {
                String sludgePumpUrl = FileUploadUtils.upload(AbvnetConfig.getEqpPath(), sludgePumpFile);
                projectEqp.setSludgePumpUrl(sludgePumpUrl);
            }
            if (airFloatFile != null && !airFloatFile.isEmpty()) {
                String airFloatUrl = FileUploadUtils.upload(AbvnetConfig.getEqpPath(), airFloatFile);
                projectEqp.setAirFloatUrl(airFloatUrl);
            }
            if (mbrMemFile != null && !mbrMemFile.isEmpty()) {
                String mbrMemUrl = FileUploadUtils.upload(AbvnetConfig.getEqpPath(), mbrMemFile);
                projectEqp.setMbrMemUrl(mbrMemUrl);
            }
            if (otherEqpFile != null && !otherEqpFile.isEmpty()) {
                String otherUrl = FileUploadUtils.upload(AbvnetConfig.getEqpPath(), otherEqpFile);
                projectEqp.setOtherEqpUrl(otherUrl);
            }
            return toAjax(projectEqpService.updateProjectEqp(projectEqp));
        } catch (Exception e) {
            log.error("更新设备运行情况失败！", e);
            return error(e.getMessage());
        }
    }
}
